#if UNITY_EDITOR || UNITY_OPENHARMONY
using UnityEngine;

namespace NativeCameraNamespace
{
    public class NCVideoPropertiesCallbackOH : MonoBehaviour
    {
        private static NCVideoPropertiesCallbackOH instance;
        private System.Action mainThreadAction = null;
        
        private NativeCamera.OnGetVideoPropertiesCallback callback;
        
        public static void Initialize(NativeCamera.OnGetVideoPropertiesCallback callback )
        {
            if( instance == null )
            {
                instance = new GameObject( "NCVideoPropertiesCallbackOH" ).AddComponent<NCVideoPropertiesCallbackOH>();
                DontDestroyOnLoad( instance.gameObject );
            }
            instance.callback = callback;
            instance.mainThreadAction = null;
        }
		
        private void Update()
        {
            if( instance.mainThreadAction != null )
            {
                try
                {
                    System.Action temp = instance.mainThreadAction;
                    instance.mainThreadAction = null;
                    temp();
                }
                finally
                {
                    Destroy( instance.gameObject );
                }
            }
        }

        private void CallOnMainThread( System.Action function )
        {
            instance.mainThreadAction = function;
        }
        
        [UnityEngine.Scripting.Preserve]
        public void OnVideoPropertiesReceived( string properties )
        {
            instance.CallOnMainThread( () => instance.callback( !string.IsNullOrEmpty( properties ) ? properties : null ) );
        }
    }
}
#endif